<template>
    <customizePickerVue :pickerValue.sync="pickerValue" :picker-column="pickerColumn" @bindChange="changePicker"/>
</template>

<script>
    import customizePickerVue from '../../../components/customize-picker/customize-picker.vue';
    import positionContainerVue from '../../../components/position-container/position-container.vue';
  	import util from '@/utils/util';

    export default {
        props: {
            value: String,
        },
        components: {
            positionContainerVue,
            customizePickerVue,
        },
        data() {
			const today = util.formatDate.format(new Date,'yyyy-MM');
            return {
                thisY: today.split('-')[0],
                today: today,
                newShow: false,
                newVal: '',
                pickerValue: [],
                pickerColumn: [
                    {
                        suffix: '年',
                        key: 'y',
                        listKey: 'y',
                        list: [],
                    },
                    {
                        suffix: '月',
                        key: 'm',
                        listKey: 'm',
                        list: [],
                    },
                ]
            };
        },
        computed: {
        },
        watch: {
            value: {
                handler(val){
                    console.log(val);
                    if (this.newVal !== val) {
                        this.newVal = val;
                        this.initPickerValue();
                    }
                },
                immediate: !0,
            },
        },
        mounted(){
            this.initPickerColumn();
        },
        methods: {
            changeShow(){
                this.$emit('update:show',this.newShow);
                this.$emit('changeShow', this.newShow);
            },
            generateSequence(count,initial = 0) {
                const init = Number(initial);
                return [...Array(count).keys()].map(i => {
                    const num = i + init;
                    const str = num < 10 ? `0${num}` : `${num}`;
                    return str;
                });
            },
            initPickerColumn(){
                const y = this.generateSequence(50, this.thisY - 49);
                const m = this.generateSequence(12,1);
                this.$set(this.pickerColumn[0],'list', y);
                this.$set(this.pickerColumn[1],'list', m);
                this.initPickerValue();
            },
            initPickerValue(){
                const lists = [];
                /* 没数据 就默认今天 */
                const newVal = this.value || this.today;
                this.pickerColumn.forEach((v,i) => {
                    const index = v.list.findIndex(item => String(item) === String(newVal.split('-')[i]));
                    lists.push(index > -1 ? index : v.list.length - 1);
                })
                this.pickerValue = lists;
            },
            resetTime(){
            },
            endChange(){
            },
			changePicker(){
				const temVal = [];
				this.pickerValue.forEach((v, i) => {
					const str = this.pickerColumn[i].list[v];
					temVal.push(str);
				});
                let title;
                const date = new Date().getTime();
                const date1 = new Date(temVal.join('-')).getTime();
                if (date1 > date) title = `毕业时间不能大于当前时间`;
                if (title) {
                    uni.showToast({
                        title,
                        icon: 'none',
                        mask: !0,
                    })
                    this
                    this.initPickerValue();
                    return false;
                }
                this.$emit('update:value',temVal.join('-'));
			},
        }
    }
</script>

<style lang="less" scoped>
</style>
